<template>
  <a-modal
    centered
    :width="750"
    class="img-preview-modalss"
    :visible="show"
    :closable="false"
    :footer="null"
    :bodyStyle="{padding: 0}"
    @cancel="close"
    :zIndex="9999"
  >
    <div v-for="(item,index) in imgList" :key="index" style="text-align:center;">
      <img
        ref="imgDataRefs"
        v-show="i == index"
        :src="item"
        class="imgwh"
        alt
      />
    </div>
    <div class="slide-left" @click="prev" v-show="showSwitch&& i!==0">
      <img src="../../assets/img/before.png" style="width:14px;margin-right:2px;" alt />
      <!-- <a-icon type="left" /> -->
    </div>
    <div class="slide-left" @click="prev" v-show="showSwitch&& i===0">
      <img src="../../assets/img/before1.png" style="width:14px;margin-right:2px;" alt />
      <!-- <a-icon type="left" /> -->
    </div>
    <div class="slide-right" @click="next" v-show="showSwitch&& i!==imgList.length-1">
      <img src="../../assets/img/next.png" style="width:14px;margin-left:2px;" alt />
    </div>
    <div class="slide-right" @click="next" v-show="showSwitch&&i===imgList.length-1">
      <img src="../../assets/img/next1.png" style="width:14px;margin-left:2px;" alt />
    </div>
    <div class="imgList">
      <img :class="i===index?'checked':''" @click="i=index" v-for="(item,index) in imgList" :key="index" :src="item" alt="">
    </div>
  </a-modal>
</template>

<script>
export default {
  data () {
    return {
      naturalHeight: 0,
      naturalWidth: 0,
      i: 0
    }
  },
  methods: {
    next () {
      if (this.i < this.imgList.length - 1) {
        this.i++
      } else {
        this.$message.newFun('当前是最后一张哦', 'warning', 2)
      }
    },
    prev () {
      if (this.i > 0) {
        this.i--
      } else {
        this.$message.newFun('当前是第一张哦', 'warning', 2)
      }
    },
    close () {
      this.$emit('showChange', false)
    },
    imgStyle (item) {
      return {
        backgroundImage: `url(${item})`,
        backgroundRepeat: 'no-repeat',
        backgroundSize: '100% 100%'
      }
    }
  },
  watch: {
    show: function (val) {
      if (val) {
        setTimeout(() => {
          this.naturalHeight = this.$refs.imgDataRefs[0].naturalHeight
          this.naturalWidth = this.$refs.imgDataRefs[0].naturalWidth
        }, 100)

        // this.$refs.carousel.goTo(0, true)
        this.i = this.index
      }
    }
  },
  props: {
    imgList: {
      type: Array,
      default: () => []
    },
    index: {
      type: Number,
      default: 0
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    showSwitch () {
      return this.imgList.length > 1
    }
  },
  model: {
    prop: 'show',
    event: 'showChange'
  }
}
</script>

<style lang="less" scoped>
.imgList{
  display: flex;
  position: fixed;
  width: 100%;
  justify-content: center;
  bottom: 20px;
  left: 0;
  img{
    width: 88px;
    height: 88px;
    margin-right: 12px;
  }
  .checked{
    width: 88px;
    height: 88px;
    margin-right: 12px;
    border: 2px solid #003482;
  }
}
.img-preview-modalss {
  text-align: center;

  .ant-modal-content {
    position: relative;
    background-color: transparent;
    background-clip: padding-box;
    border: 0;
    border-radius: 4px;
    box-shadow: none;
    // box-shadow: 0 4px 12px rgb(0 0 0 / 0%);
    // -webkit-box-shadow: 0 4px 12px rgb(0 0 0 / 0%);

    pointer-events: auto;
  }
  .imageDataH {
    height: 800px;
  }
  .imageDataW {
    width: 750px;
  }

  .imgwh{
    max-width: 750px;
    max-height: 600px;
  }
  .slide-right {
    right: -150px;
  }
  .slide-left {
    left: -150px;
  }
  .slide-left,
  .slide-right {
    position: absolute;
    top: calc(50% - 26px);
    width: 52px;
    height: 52px;
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    cursor: pointer;
    background: rgba(51, 51, 51, 0.4);

    .anticon {
      font-size: 22px;
      line-height: 22px;
      color: #fff;
    }
  }
}
</style>
